/**
 * Created by jsroads on 2023/06/05 16:03
 * Note:
 */

import { Size, sys, view } from "cc";

enum MiniBrowserType {
    WX = "wx",
    DY = "dy",
    QQ = "qq",
    HW = "hw",
    OPPO = "oppo",
    WEB = "web",
    VIVO = "vivo",
    ALIPAY = "my"
}

export class Browser {
    /** 是否Android 环境。*/
    public static onAndroid: boolean = false;
    /** 是否Web 环境。*/
    public static onWeb: boolean = false;
    /** 是否Simulator 环境。*/
    public static onSimulator: boolean = false;
    /** 是否iOS 环境。*/
    public static onIOS: boolean = false;
    /**运行系统 */
    public static os: string = "unknown";

    /*宿主环境*/
    private static _browserType: string = "unknown";
    // /*幸运模式 可以跳过广告 特色服务*/
    // public static isLuckyBoy: boolean = false;

    static get browserType(): string {
        return this._browserType;
    }

    /** 微信小游戏*/
    static get onWXMiniGame(): boolean {
        return sys.platform === sys.Platform.WECHAT_GAME;
    }

    /** QQ小游戏*/
    static get onQQGame(): boolean {
        // @ts-ignore
        return this.onWXMiniGame && typeof qq !== "undefined";
    }

    /** 华为小游戏*/
    static get onHWMiniGame(): boolean {
        return sys.platform === sys.Platform.HUAWEI_QUICK_GAME;
    }

    /** 字节跳动（头条）小游戏*/
    static get onDYMiniGame(): boolean {
        return sys.platform === sys.Platform.BYTEDANCE_MINI_GAME;
    }

    /** oppo小游戏*/
    static get onOppoMiniGame(): boolean {
        return sys.platform === sys.Platform.OPPO_MINI_GAME;
    }

    /** oppo小游戏*/
    static get onVivoMiniGame(): boolean {
        return sys.platform === sys.Platform.VIVO_MINI_GAME;
    }

    static get onAlipayMiniGame(): boolean {
        return sys.platform === sys.Platform.ALIPAY_MINI_GAME;
    }

    /** 小游戏Android 平台*/
    static get onMiniAndroidGame(): boolean {
        return this.onMiniGame && sys.platform === sys.Platform.ANDROID;
    }

    /** 小游戏ios 平台*/
    static get onMiniIOSGame(): boolean {
        return this.onMiniGame && sys.platform === sys.Platform.IOS;
    }



    /** 国内App小游戏IOS 平台*/
    static get onAppIOS(): boolean {
        return sys.platform === sys.Platform.IOS && !this.jsbEmpty;
    }

    /** 是否小游戏环境（微信，头条*/
    public static get onMiniGame(): boolean {
        return (this.onWXMiniGame || this.onDYMiniGame || this.onQQGame || this.onHWMiniGame || this.onOppoMiniGame || this.onVivoMiniGame || this.onAlipayMiniGame);
    }

    public static get isDebugMode(): boolean {
        return false;
    }

    /**
     * 目前没有找到 代替 winSize 先这样设置 后面统一修改
     */
    public static get winSize(): Size {
        return view.getVisibleSize();
    }

    public static get isSameToMiniGame(): boolean {
        return true;
    }

    /**
     * 原生JSB对象为空 比如在 网页版本 手机模式下 需要此判断
     * @private
     */
    public static get jsbEmpty(): boolean {
        return typeof jsb === "undefined";
    }

    public static init(): void {
        // //强制为抖音小游戏
        // Browser.browserType = "dy";
        // /*初始化小游戏专用sdk*/
        // await MiniPlatform.init();
        // return Promise.resolve()
        if (sys.isNative) {
            console.log("本地平台");
            if (sys.isMobile) {
                console.log("本地移动平台");
                if (sys.platform == sys.Platform.ANDROID) {
                    console.log("本地Android平台");
                    Browser.os = "android";
                    Browser.onAndroid = true;
                } else if (sys.platform == sys.Platform.IOS) {
                    console.log("本地ios平台");
                    Browser.os = "ios";
                    Browser.onIOS = true;

                }
            } else {
                console.log("未知本地平台");
                Browser._browserType = "Simulator";
                Browser.onSimulator = true;
            }
        } else {
            if (Browser.onQQGame) {
                // 必须先判断 QQ 平台，在微信平台判断之前
                Browser._browserType = MiniBrowserType.QQ;
                console.log("QQ小游戏平台");
            } else if (Browser.onWXMiniGame) {
                Browser._browserType = MiniBrowserType.WX;
                console.log("微信小游戏平台");
            } else if (Browser.onDYMiniGame) {
                Browser._browserType = MiniBrowserType.DY;
                console.log("头条小游戏平台");
            } else if (Browser.onOppoMiniGame) {
                Browser._browserType = MiniBrowserType.OPPO;
                console.log("oppo小游戏平台");
            } else if (Browser.onVivoMiniGame) {
                Browser._browserType = MiniBrowserType.VIVO;
                console.log("vivo小游戏平台");
            } else if (Browser.onHWMiniGame) {
                Browser._browserType = MiniBrowserType.HW;
                console.log("华为小游戏平台");
            } else if (Browser.onAlipayMiniGame) {
                Browser._browserType = MiniBrowserType.ALIPAY;
                console.log("支付宝小游戏平台");
            } else {
                Browser.onWeb = true;
                Browser._browserType = MiniBrowserType.WEB;
                console.log("本地Web平台");
            }
        }
    }

}
